﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class OppOrderRuleDetailDA : IOppOrderRuleDetailDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string LINE_ID = "@LINE_ID";
        private string OPP_ORDER_RULE_ID = "@OPP_ORDER_RULE_ID";
        private string OPP_ORDER_STRATEGY = "@OPP_ORDER_STRATEGY";
        private string ORDER_DATE_END_DAYS = "@ORDER_DATE_END_DAYS";
        private string ORDER_DATE_START_DAYS = "@ORDER_DATE_START_DAYS";
        private string ORDER_TYPE_INCLUDE = "@ORDER_TYPE_INCLUDE";
        private string PRIORITY_FROM = "@PRIORITY_FROM";
        private string PRIORITY_TO = "@PRIORITY_TO";
        private string REQ_SHIP_DATE_END_DAYS = "@REQ_SHIP_DATE_END_DAYS";
        private string REQ_SHIP_DATE_START_DAYS = "@REQ_SHIP_DATE_START_DAYS";
        private string SQL_CHECK_OPP_ORDER_RULE_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@OPP_ORDER_RULE_DETAIL WHERE OPP_ORDER_RULE_ID = @OPP_ORDER_RULE_ID AND LINE_ID = @LINE_ID ";
        private string SQL_DELETE_OPP_ORDER_RULE_DETAIL = " DELETE FROM @_@OPP_ORDER_RULE_DETAIL WHERE OPP_ORDER_RULE_ID = @OPP_ORDER_RULE_ID  ";
        private string SQL_INSERT_OPP_ORDER_RULE_DETAIL = " INSERT INTO @_@OPP_ORDER_RULE_DETAIL ( WH_ID, OPP_ORDER_RULE_ID, LINE_ID, OPP_ORDER_STRATEGY, PRIORITY_FROM, PRIORITY_TO, ORDER_DATE_START_DAYS, ORDER_DATE_END_DAYS, REQ_SHIP_DATE_START_DAYS, REQ_SHIP_DATE_END_DAYS, ORDER_TYPE_INCLUDE, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @OPP_ORDER_RULE_ID, @LINE_ID, @OPP_ORDER_STRATEGY, @PRIORITY_FROM, @PRIORITY_TO, @ORDER_DATE_START_DAYS, @ORDER_DATE_END_DAYS, @REQ_SHIP_DATE_START_DAYS, @REQ_SHIP_DATE_END_DAYS, @ORDER_TYPE_INCLUDE, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_OPP_ORDER_RULE_DETAIL = " SELECT  ROWID, WH_ID, OPP_ORDER_RULE_ID, LINE_ID, OPP_ORDER_STRATEGY, PRIORITY_FROM, PRIORITY_TO, ORDER_DATE_START_DAYS, ORDER_DATE_END_DAYS, REQ_SHIP_DATE_START_DAYS, REQ_SHIP_DATE_END_DAYS, ORDER_TYPE_INCLUDE, IS_ACTIVE, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@OPP_ORDER_RULE_DETAIL ";
        private string SQL_UPDATE_OPP_ORDER_RULE_DETAIL = " UPDATE @_@OPP_ORDER_RULE_DETAIL SET WH_ID = @WH_ID, OPP_ORDER_STRATEGY = @OPP_ORDER_STRATEGY, PRIORITY_FROM = @PRIORITY_FROM, PRIORITY_TO = @PRIORITY_TO, ORDER_DATE_START_DAYS = @ORDER_DATE_START_DAYS, ORDER_DATE_END_DAYS = @ORDER_DATE_END_DAYS, REQ_SHIP_DATE_START_DAYS = @REQ_SHIP_DATE_START_DAYS, REQ_SHIP_DATE_END_DAYS = @REQ_SHIP_DATE_END_DAYS, ORDER_TYPE_INCLUDE = @ORDER_TYPE_INCLUDE, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE OPP_ORDER_RULE_ID = @OPP_ORDER_RULE_ID AND LINE_ID = @LINE_ID  ";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckOppOrderRuleDetailIDUnique(string oppOrderRuleID, string lineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OPP_ORDER_RULE_ID", oppOrderRuleID), new SqlParameter("@LINE_ID", lineID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_OPP_ORDER_RULE_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckOppOrderRuleDetailIDUnique(DataBase dataBase, DbTransaction tran, string oppOrderRuleID, string lineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OPP_ORDER_RULE_ID", oppOrderRuleID), new SqlParameter("@LINE_ID", lineID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_OPP_ORDER_RULE_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteOppOrderRuleDetail(List<string> oppOrderRuleID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < oppOrderRuleID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OPP_ORDER_RULE_ID", oppOrderRuleID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_OPP_ORDER_RULE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteOppOrderRuleDetail(List<string> oppOrderRuleDetailID, List<string> lineIDs, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < oppOrderRuleDetailID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OPP_ORDER_RULE_ID", oppOrderRuleDetailID[j]), new SqlParameter("@LINE_ID", lineIDs[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_OPP_ORDER_RULE_DETAIL.Replace("@_@", whLoginID[i]) + "AND LINE_ID = @LINE_ID ", paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteOppOrderRuleDetail(DataBase dataBase, DbTransaction tran, List<string> oppOrderRuleID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < oppOrderRuleID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OPP_ORDER_RULE_ID", oppOrderRuleID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_OPP_ORDER_RULE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteOppOrderRuleDetail(DataBase dataBase, DbTransaction tran, List<string> oppOrderRuleDetailID, List<string> lineIDs, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < oppOrderRuleDetailID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OPP_ORDER_RULE_ID", oppOrderRuleDetailID[j]), new SqlParameter("@LINE_ID", lineIDs[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_OPP_ORDER_RULE_DETAIL.Replace("@_@", whLoginID[i]) + "AND LINE_ID = @LINE_ID ", paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllOppOrderRuleDetail(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_OPP_ORDER_RULE_DETAIL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllOppOrderRuleDetail(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_OPP_ORDER_RULE_DETAIL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public OppOrderRuleDetailInfo GetOppOrderRuleDetailByID(string opporderruledetailID, string lineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_OPP_ORDER_RULE_DETAIL.Replace("@_@", whLoginID) + " WHERE OPP_ORDER_RULE_ID = @OPP_ORDER_RULE_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OPP_ORDER_RULE_ID", opporderruledetailID), new SqlParameter("@LINE_ID", lineID) };
            OppOrderRuleDetailInfo oppOrderRuleDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    oppOrderRuleDetailInfo = new OppOrderRuleDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["OPP_ORDER_RULE_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["OPP_ORDER_STRATEGY"].ToString(), reader["PRIORITY_FROM"].ToString(), reader["PRIORITY_TO"].ToString(), (reader["ORDER_DATE_START_DAYS"].ToString() != "") ? int.Parse(reader["ORDER_DATE_START_DAYS"].ToString()) : 0, (reader["ORDER_DATE_END_DAYS"].ToString() != "") ? int.Parse(reader["ORDER_DATE_END_DAYS"].ToString()) : 0, (reader["REQ_SHIP_DATE_START_DAYS"].ToString() != "") ? int.Parse(reader["REQ_SHIP_DATE_START_DAYS"].ToString()) : 0, (reader["REQ_SHIP_DATE_END_DAYS"].ToString() != "") ? int.Parse(reader["REQ_SHIP_DATE_END_DAYS"].ToString()) : 0, reader["ORDER_TYPE_INCLUDE"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return oppOrderRuleDetailInfo;
        }

        public OppOrderRuleDetailInfo GetOppOrderRuleDetailByID(DataBase dataBase, DbTransaction tran, string oppOrderRuleID, string lineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_OPP_ORDER_RULE_DETAIL.Replace("@_@", whLoginID) + " WHERE OPP_ORDER_RULE_ID = @OPP_ORDER_RULE_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OPP_ORDER_RULE_ID", oppOrderRuleID), new SqlParameter("@LINE_ID", lineID) };
            OppOrderRuleDetailInfo oppOrderRuleDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                oppOrderRuleDetailInfo = new OppOrderRuleDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["OPP_ORDER_RULE_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["OPP_ORDER_STRATEGY"].ToString(), reader["PRIORITY_FROM"].ToString(), reader["PRIORITY_TO"].ToString(), (reader["ORDER_DATE_START_DAYS"].ToString() != "") ? int.Parse(reader["ORDER_DATE_START_DAYS"].ToString()) : 0, (reader["ORDER_DATE_END_DAYS"].ToString() != "") ? int.Parse(reader["ORDER_DATE_END_DAYS"].ToString()) : 0, (reader["REQ_SHIP_DATE_START_DAYS"].ToString() != "") ? int.Parse(reader["REQ_SHIP_DATE_START_DAYS"].ToString()) : 0, (reader["REQ_SHIP_DATE_END_DAYS"].ToString() != "") ? int.Parse(reader["REQ_SHIP_DATE_END_DAYS"].ToString()) : 0, reader["ORDER_TYPE_INCLUDE"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return oppOrderRuleDetailInfo;
        }

        public DataSet GetOppOrderRuleDetailByQueryList(List<string> sqlWhere, OppOrderRuleDetailQueryEntity opporderruledetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_OPP_ORDER_RULE_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!opporderruledetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, opporderruledetailQuery.CurrentPage, opporderruledetailQuery.PageSize, opporderruledetailQuery.SortField, opporderruledetailQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetOppOrderRuleDetailByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, OppOrderRuleDetailQueryEntity opporderruledetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_OPP_ORDER_RULE_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, opporderruledetailQuery.CurrentPage, opporderruledetailQuery.PageSize, opporderruledetailQuery.SortField, opporderruledetailQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public List<OppOrderRuleDetailInfo> GetOppOrderRuleDetailListByID(string oppOrderRuleID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_OPP_ORDER_RULE_DETAIL.Replace("@_@", whLoginID) + " WHERE OPP_ORDER_RULE_ID = @OPP_ORDER_RULE_ID ORDER BY LINE_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter(this.OPP_ORDER_RULE_ID, oppOrderRuleID) };
            List<OppOrderRuleDetailInfo> oppOrderRuleDetailInfos = new List<OppOrderRuleDetailInfo>();
            OppOrderRuleDetailInfo oppOrderRuleDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    oppOrderRuleDetailInfo = new OppOrderRuleDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["OPP_ORDER_RULE_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["OPP_ORDER_STRATEGY"].ToString(), reader["PRIORITY_FROM"].ToString(), reader["PRIORITY_TO"].ToString(), (reader["ORDER_DATE_START_DAYS"].ToString() != "") ? int.Parse(reader["ORDER_DATE_START_DAYS"].ToString()) : 0, (reader["ORDER_DATE_END_DAYS"].ToString() != "") ? int.Parse(reader["ORDER_DATE_END_DAYS"].ToString()) : 0, (reader["REQ_SHIP_DATE_START_DAYS"].ToString() != "") ? int.Parse(reader["REQ_SHIP_DATE_START_DAYS"].ToString()) : 0, (reader["REQ_SHIP_DATE_END_DAYS"].ToString() != "") ? int.Parse(reader["REQ_SHIP_DATE_END_DAYS"].ToString()) : 0, reader["ORDER_TYPE_INCLUDE"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                    oppOrderRuleDetailInfos.Add(oppOrderRuleDetailInfo);
                }
            }
            return oppOrderRuleDetailInfos;
        }

        public List<OppOrderRuleDetailInfo> GetOppOrderRuleDetailListByID(DataBase dataBase, DbTransaction tran, string oppOrderRuleID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_OPP_ORDER_RULE_DETAIL.Replace("@_@", whLoginID) + " WHERE OPP_ORDER_RULE_ID = @OPP_ORDER_RULE_ID ORDER BY LINE_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter(this.OPP_ORDER_RULE_ID, oppOrderRuleID) };
            List<OppOrderRuleDetailInfo> oppOrderRuleDetailInfos = new List<OppOrderRuleDetailInfo>();
            OppOrderRuleDetailInfo oppOrderRuleDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            while (reader.Read())
            {
                DateTime CS00002;
                oppOrderRuleDetailInfo = new OppOrderRuleDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["OPP_ORDER_RULE_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["OPP_ORDER_STRATEGY"].ToString(), reader["PRIORITY_FROM"].ToString(), reader["PRIORITY_TO"].ToString(), (reader["ORDER_DATE_START_DAYS"].ToString() != "") ? int.Parse(reader["ORDER_DATE_START_DAYS"].ToString()) : 0, (reader["ORDER_DATE_END_DAYS"].ToString() != "") ? int.Parse(reader["ORDER_DATE_END_DAYS"].ToString()) : 0, (reader["REQ_SHIP_DATE_START_DAYS"].ToString() != "") ? int.Parse(reader["REQ_SHIP_DATE_START_DAYS"].ToString()) : 0, (reader["REQ_SHIP_DATE_END_DAYS"].ToString() != "") ? int.Parse(reader["REQ_SHIP_DATE_END_DAYS"].ToString()) : 0, reader["ORDER_TYPE_INCLUDE"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                oppOrderRuleDetailInfos.Add(oppOrderRuleDetailInfo);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return oppOrderRuleDetailInfos;
        }

        public int InsertOppOrderRuleDetail(OppOrderRuleDetailInfo oppOrderRuleDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    oppOrderRuleDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_OppOrderRuleDetail_Parameters(oppOrderRuleDetailInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_OPP_ORDER_RULE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertOppOrderRuleDetail(DataBase dataBase, DbTransaction tran, OppOrderRuleDetailInfo oppOrderRuleDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    oppOrderRuleDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_OppOrderRuleDetail_Parameters(oppOrderRuleDetailInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_OPP_ORDER_RULE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_OppOrderRuleDetail_Parameters(OppOrderRuleDetailInfo oppOrderRuleDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.OPP_ORDER_RULE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.LINE_ID, SqlDbType.Int, 4), new SqlParameter(this.OPP_ORDER_STRATEGY, SqlDbType.VarChar, 20), new SqlParameter(this.PRIORITY_FROM, SqlDbType.VarChar, 10), new SqlParameter(this.PRIORITY_TO, SqlDbType.VarChar, 10), new SqlParameter(this.ORDER_DATE_START_DAYS, SqlDbType.Int, 4), new SqlParameter(this.ORDER_DATE_END_DAYS, SqlDbType.Int, 4), new SqlParameter(this.REQ_SHIP_DATE_START_DAYS, SqlDbType.Int, 4), new SqlParameter(this.REQ_SHIP_DATE_END_DAYS, SqlDbType.Int, 4), new SqlParameter(this.ORDER_TYPE_INCLUDE, SqlDbType.VarChar, 10), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(oppOrderRuleDetailInfo.WhID))
            {
                paramArray[0].Value = oppOrderRuleDetailInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = oppOrderRuleDetailInfo.OppOrderRuleID;
            paramArray[2].Value = oppOrderRuleDetailInfo.LineID;
            paramArray[3].Value = oppOrderRuleDetailInfo.OppOrderStrategy;
            if (!string.IsNullOrEmpty(oppOrderRuleDetailInfo.PriorityFrom))
            {
                paramArray[4].Value = oppOrderRuleDetailInfo.PriorityFrom;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(oppOrderRuleDetailInfo.PriorityTo))
            {
                paramArray[5].Value = oppOrderRuleDetailInfo.PriorityTo;
            }
            else
            {
                paramArray[5].Value = DBNull.Value;
            }
            paramArray[6].Value = oppOrderRuleDetailInfo.OrderDateStartDays;
            paramArray[7].Value = oppOrderRuleDetailInfo.OrderDateEndDays;
            paramArray[8].Value = oppOrderRuleDetailInfo.ReqShipDateStartDays;
            paramArray[9].Value = oppOrderRuleDetailInfo.ReqShipDateEndDays;
            if (!string.IsNullOrEmpty(oppOrderRuleDetailInfo.OrderTypeInclude))
            {
                paramArray[10].Value = oppOrderRuleDetailInfo.OrderTypeInclude;
            }
            else
            {
                paramArray[10].Value = DBNull.Value;
            }
            paramArray[11].Value = oppOrderRuleDetailInfo.IsActive;
            paramArray[12].Value = oppOrderRuleDetailInfo.CreatedBy;
            paramArray[13].Value = oppOrderRuleDetailInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateOppOrderRuleDetail(OppOrderRuleDetailInfo oppOrderRuleDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    oppOrderRuleDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_OppOrderRuleDetail_Parameters(oppOrderRuleDetailInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_OPP_ORDER_RULE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateOppOrderRuleDetail(DataBase dataBase, DbTransaction tran, OppOrderRuleDetailInfo oppOrderRuleDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    oppOrderRuleDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_OppOrderRuleDetail_Parameters(oppOrderRuleDetailInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_OPP_ORDER_RULE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

